<?php

/*
 *  Registration form for Coliberator
 *  Copyright (C) 2014, 2015 Fundația Ceata - http://ceata.org
 *  
 *  Author: Victor Nițu (vnitu@ceata.org)
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *  
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

session_start();

require_once "./securimage/securimage.php";

$filename = dirname(__FILE__) . '/coliberator2015.db';

$db  = new SQLite3($filename);
$securimage = new Securimage();


if ($securimage->check($_POST['captcha_code']) == false) {
  // the code was incorrect
  // you should handle the error so that the form processor doesn't continue

  // or you can use the following code if there is no validation or you do not know how
  echo "The security code entered was incorrect.<br /><br />";
  echo "Please go <a href='javascript:history.go(-1)'>back</a> and try again.";
  exit;
}

//$db->exec($query_string);

//$result     = $db->query($query_string);
//$row        = $result->fetchArray();

foreach ($_POST as $key => $value) {
    $$key = $value;
}

if (!isset($anoncheck)) {
    $fullname = filter_var($fullname, FILTER_SANITIZE_STRING);
} else {
    $fullname = 'n/a';
}

$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$skill = filter_var($skill, FILTER_SANITIZE_STRING);

$searchQ = "SELECT * FROM participants WHERE email = '$email';";
$insertQ = "INSERT INTO participants (fullname, email, skill) 
    VALUES ('$fullname', '$email', '$skill');";

$res = $db->query($searchQ);
$rows = $res->fetchArray();
if ($rows['email'] == $email) {
    echo "
<script type='text/javascript'>
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
    alert('We already have this email!');
    window.location = 'http://coliberator.ro/form';
// @license-end
</script>
        ";
} else {
    $message = "
    Nume: $fullname\r\n
    Email: $email\r\n
    Ocupația: $skill\r\n
    ";
    $db->exec($insertQ);
    mail('coliberator@ceata.org', 'Confirmare de înregistrare la Coliberator 2015', $message);
    echo "
<script type='text/javascript'>
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
    alert('Multumesc! / Thank you!');
    window.location = 'http://coliberator.ro';
// @license-end
</script>
        ";
}
